package cn.hpclub.server.controller.h5.esopdemo;

import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.H5ApiConstant;
import cn.hpclub.server.controller.h5.BaseH5Controller;
import cn.hpclub.server.model.esopdemo.Idc;

public class OpponentController extends BaseH5Controller{

    public void addIdc(){
        JSONObject result = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL, null);
        try{
            // 获取前端Ajax参数并保存到Db中

            Idc idc = new Idc();
            idc.set("bus_type", getPara("bus_type"));
            idc.set("group_name", getPara("group_name"));
            idc.set("group_no", getPara("group_no"));
            idc.set("operator", getPara("operator"));
            idc.set("k1", getPara("k1"));
            idc.set("k2", getPara("k2"));
            idc.set("k3", getPara("k3"));
            idc.set("k4", getPara("k4"));
            idc.set("k5", getPara("k5"));
            idc.set("k6", getPara("k6"));
            idc.set("k7", getPara("k7"));
            idc.set("k8", getPara("k8"));
            idc.set("k9", getPara("k9"));
            idc.set("k10", getPara("k10"));
            idc.set("k11", getPara("k11"));
            idc.set("k12", getPara("k12"));
            idc.set("k13", getPara("k13"));
            idc.set("k14", getPara("k14"));
            idc.set("k15", getPara("k15"));
            idc.set("created_by", getPara("created_by"));
            idc.set("created_time", new Date());
            idc.save();

            result = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, null);
            renderText(result.toString());
        }
        catch(Exception e){
            result = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, H5ApiConstant.Msg.EXCEPTION, null);
            e.printStackTrace();
            renderText(result.toString());
        }
    }

    public void getIdcDetailsById(){
        JSONObject result = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL, null);
        try{
            String id = getPara("id");
            Model<Idc> idcDetails = Idc.dao.findById(id);
            JSONObject json = new JSONObject(JsonKit.toJson(idcDetails));

            result = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, json);
            renderText(result.toString());
        }
        catch(Exception e){
            result = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, H5ApiConstant.Msg.EXCEPTION, null);
            e.printStackTrace();
            renderText(result.toString());
        }
    }

    public void deleteOpponent(){
        int id = getParaToInt("id");
        Idc idc = getModel(Idc.class);
        idc.deleteById(id);
        JSONObject result = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, "");
        renderText(result.toString());
    }

    public void getOpponentList(){
        JSONObject result = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL, null);
        try{
            String group_name = getPara("group_name");
            String group_no = getPara("group_no");
            String operator = getPara("operator");

            List<Record> idcList = Idc.dao.getOpponentList(group_name, group_no, operator);
            JSONArray json = new JSONArray(JsonKit.toJson(idcList));

            result = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, json);
            renderText(result.toString());
        }
        catch(Exception e){
            result = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, H5ApiConstant.Msg.EXCEPTION, null);
            e.printStackTrace();
            renderText(result.toString());
        }
    }

    public void addProgress(){
        JSONObject result = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL, null);
        try{

            Idc idc = new Idc();
            idc.set("id", getPara("id"));
            idc.set("k4", getPara("progress"));
            idc.set("created_by", getPara("created_by"));
            idc.set("created_time", new Date());
            idc.update();

            result = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, null);
            renderText(result.toString());
        }
        catch(Exception e){
            result = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, H5ApiConstant.Msg.EXCEPTION, null);
            e.printStackTrace();
            renderText(result.toString());
        }
    }

    public void editIdc(){
        JSONObject result = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL, null);
        try{

            Idc idc = new Idc();
            idc.set("id", getPara("id"));
            idc.set("bus_type", getPara("bus_type"));
            idc.set("group_name", getPara("group_name"));
            idc.set("group_no", getPara("group_no"));
            idc.set("operator", getPara("operator"));
            idc.set("k1", getPara("k1"));
            idc.set("k2", getPara("k2"));
            idc.set("k3", getPara("k3"));
            idc.set("k4", getPara("k4"));
            idc.set("k5", getPara("k5"));
            idc.set("k6", getPara("k6"));
            idc.set("k7", getPara("k7"));
            idc.set("k8", getPara("k8"));
            idc.set("k9", getPara("k9"));
            idc.set("k10", getPara("k10"));
            idc.set("k11", getPara("k11"));
            idc.set("k12", getPara("k12"));
            idc.set("k13", getPara("k13"));
            idc.set("k14", getPara("k14"));
            idc.set("k15", getPara("k15"));
            idc.set("created_by", getPara("created_by"));
            idc.set("created_time", new Date());
            idc.update();

            result = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, null);
            renderText(result.toString());
        }
        catch(Exception e){
            result = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, H5ApiConstant.Msg.EXCEPTION, null);
            e.printStackTrace();
            renderText(result.toString());
        }
    }

    public void getOpportunityList(){
        JSONObject result = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL, null);
        try{
            String opp_id = getPara("opp_id");
            String opp_type = getPara("opp_type");
            String opp_part = getPara("opp_part");
            String group_no = getPara("grpId");

            List<Record> idcList = Idc.dao.getOpportunityList(opp_id, opp_type, opp_part, group_no);
            JSONArray json = new JSONArray(JsonKit.toJson(idcList));

            result = setResponseObj(H5ApiConstant.ResultCode.SUCCESS_CODE, H5ApiConstant.Msg.SUCCESS, json);
            renderText(result.toString());
        }
        catch(Exception e){
            result = setResponseObj(H5ApiConstant.ResultCode.EXCEPTION_CODE, H5ApiConstant.Msg.EXCEPTION, null);
            e.printStackTrace();
            renderText(result.toString());
        }
    }
}
